<?php
require_once(CORE_DIR.'Common.php');
require_once(CORE_DIR.'mappers/DailyTaskMapper.php');

class dailytaskModel extends Model{

	function __construct(){
		$this->conn = $this->getConnection();
	}
	
	function addDailyTask($taskId, $doneStatus, $date){
		$query = "DELETE FROM `dailytask` WHERE taskId = %d";
		$query = $this->format($query, array($taskId));
		$this->execute($query);
		
		if (empty($date))
		{
			$query = "insert into `dailytask` (taskId, date, doneStatus) values (%d, now(), %d)";
		}
		else
		{
			$query = "insert into `dailytask` (taskId, date, doneStatus) values (%d, '%s', %d)";
		}
				
		$query = $this->format($query, array($taskId, $date, $doneStatus));
		$this->execute($query);
		return mysqli_insert_id($this->conn);
	}
	
	function updateDailyTask($taskId, $doneStatus){
		$query = "update `dailytask` set doneStatus=%d where taskId=%d";
		$query = $this->format($query, array($doneStatus, $taskId));		
		return $this->execute($query);		
	}
	
	function moveDailyTask($taskId, $date){
		$query = "update `dailytask` set date='%s' where taskId=%d";
		$query = $this->format($query, array($date, $taskId));		
		return $this->execute($query);		
	}	
	
	function removeDailyTask($taskId) {
		$query = "DELETE FROM dailytask WHERE taskId=%d";
		$query = $this->format($query, array($taskId));
		return $this->execute($query);
	}
	
	function getDailyTaskDate($taskId){
		$query = "SELECT * FROM dailytask WHERE taskId=%d AND doneStatus=0";
		$query = $this->format($query, array($taskId));		
		$list = $this->execute($query, new DailyTaskMapper());
		if (is_null($list[0]))
			return 0;
			
		return $list[0]->getDate();
	}	
}
?>
